﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.WinForm.Resources.DBObjects;
using Gedea.Model.Entities;
using Gedea.Service;

namespace Gedea.WinForm.UserControls
{
    public partial class AreaControl : UserControl
    {
        public List<Control> myControls = new List<Control>();
        private Delegate btnCancelDeleg, btnOkDeleg;
        private bool isEditable;

        public AreaControl(Delegate btnCancel, Delegate btnOk, bool isEditableForm)
        {
            InitializeComponent();

            btnCancelDeleg = btnCancel;
            btnOkDeleg = btnOk;
            isEditable = isEditableForm;

        }

        private void AreaControl_Load(object sender, EventArgs e)
        {
            LoadUser();

            label1.Text = AreaStrings.name;
            label2.Text = AreaStrings.debitUnivocalReference;
            myControls.Add(textBox1);
            myControls.Add(textBox2);
            label3.Text = CountCharacters(textBox1, 100);
            label4.Text = CountCharacters(textBox2, 7);
            ShowErrors();

        }

        #region textChanged
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ValidateFirst();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ValidateSecond();
        }

        #endregion

        #region ValidateTextBox

        private void ValidateFirst()
        {
            MaxLengthValidation(textBox1, 100, "Se deben ingresar por lo menos 1 caracter y hasta {0}");

            label3.Text = CountCharacters(textBox1, 100);
        }

        private void ValidateSecond()
        {
            MaxLengthValidation(textBox2, 7, "Se deben ingresar {0} caracteres");

            label4.Text = CountCharacters(textBox2, 7);
        }

        #endregion

        #region Validation
        private void MaxLengthValidation(Control control, int ctrLength, string msg)
        {
            if (control.Text.Length > ctrLength || control.Text.Length == 0)
            {
                errorProvider1.SetError(control, string.Format(msg, ctrLength));
            }
            else
                errorProvider1.SetError(control, null);

            ShowErrors();
        }

        #endregion

        #region Errors
        private void ShowErrors()
        {
            string test = "";

            test += SetErrorFormat(textBox1);
            test += SetErrorFormat(textBox2);

            label5.Text = "Listado de errores: \n";
            label5.Text += test;

            if (test != "")
            {
                label5.Visible = true;
            }
            else
            {
                label5.Visible = false;
            }
            ValidateButton(test);
        }

        private string SetErrorFormat(Control control)
        {
            string res;

            if (errorProvider1.GetError(control) != "")
            {
                res = string.Format("- {0} \n", errorProvider1.GetError(control));
            }
            else
                res = "";
            return res;

        }

        #endregion

        #region CommonMethods
        private string CountCharacters(Control control, int ctrLength)
        {
            return string.Format("({0}/{1})", control.Text.Length, ctrLength);
        }

        private void ValidateButton(string test)
        {
            if (test == "" && myControls.Where(x => x.Text.Length == 0).Count() == 0)
            {
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            btnCancelDeleg.DynamicInvoke();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnOkDeleg.DynamicInvoke(GenerateViewArea());
        }

        private Area GenerateViewArea()
        {
            Area area = new Area();

            area.Name = textBox1.Text;
            area.DebitUnivocalReference = textBox2.Text;

            return area;

        }

        private void LoadUser()
        {
            if (isEditable)
            {
                button1.Text = "Editar";
            }

        }        
        
    }
}
